import { fetchApiWithGet, fetchApiWithPost } from '../utils/index'
import { FETCH_CART_GOODS_SUCC, FETCH_CART_GOODS_FAIL, ADD_GOODS_CART_SUCC, ADD_GOODS_CART_FAIL , DELETE_CART_GOODS_SUCC, DELETE_CART_GOODS_FAIL, CHANGE_GOODS_NUM_SUCC, CHANGE_GOODS_NUM_FAIL, CLEAN_CART_SUCC, CLEAN_CART_FAIL } from '../contants/shoppingCart'

var publicUrl = 'http://fengke.net:8081/shop/';
var userId = localStorage.getItem('session');

export function fetchCartGoods() {
	var url = `${publicUrl}cart_show?userId=${userId}&num=20`;
	return dispatch => {
		fetchApiWithGet(url, dispatch, FETCH_CART_GOODS_SUCC, FETCH_CART_GOODS_FAIL)
	}
}

export function addShoppingCart(goodsId,goodsNum, colorId, sizeId) {
	var url = `${publicUrl}cart_add`;
	var argus = {
		userId: userId,
		goodsId: goodsId,
		goodsNum: goodsNum,
		colorId: colorId,
		sizeId: sizeId
	};
	return dispatch => {
		fetchApiWithPost(url, argus, dispatch, ADD_GOODS_CART_SUCC, ADD_GOODS_CART_FAIL)
	}
}

export function deleteCartGoods(cartId) {
	var url = `${publicUrl}cart_delete`;
	var argus = {
		userId: userId,
		cartId: cartId
	};
	return dispatch => {
		fetchApiWithPost(url, argus, dispatch, DELETE_CART_GOODS_SUCC, DELETE_CART_GOODS_FAIL)
	}
}

export function changeGoodsNum(num,cartId) {
	var url = `${publicUrl}cart_update`;
	var argus = {
		userId: userId,
		num: num,
		cartId: cartId
	};
	return dispatch => {
		fetchApiWithPost(url, argus, dispatch, CHANGE_GOODS_NUM_SUCC, CHANGE_GOODS_NUM_FAIL)
	}
}

export function cleanShoppingCart() {
	var url = `${publicUrl}cart_clean`;
	var argus = {
		userId: userId
	};
	return dispatch => {
		fetchApiWithPost(url, argus, dispatch, CLEAN_CART_SUCC, CLEAN_CART_FAIL)
	}
}


